package com.jf.car.dao.impl;


import com.jf.car.bean.Department;
import com.jf.car.dao.DepartmentDao;
import com.jf.car.util.BaseDao;
import com.jf.car.util.MyUtils;
import com.jf.car.util.PageInfo;

import java.util.ArrayList;
import java.util.List;

public class DepartmentDaoImpl implements DepartmentDao {
    @Override
    public List<Department> getAll() {
        String sql = "select * from department";
        return BaseDao.baseQuery(Department.class,sql);
    }

    @Override
    public List<Department> getPage(PageInfo pageInfo, Department department) {
        String sql = "select * from department where 1 = 1 ";
        List<Object> objs = new ArrayList<>();
        if (MyUtils.isStringNotEmpty(department.getName())){
            sql+="and name = ? ";
            objs.add(department.getName());
        }
        sql+="order by id desc limit ?,?";
        objs.add(pageInfo.getOffset());
        objs.add(pageInfo.getPageSize());
        return BaseDao.baseQuery(Department.class,sql,objs);
    }

    @Override
    public int getCount(Department department) {
        String sql = "select count(id) from department where 1 = 1 ";
        List<Object> objs = new ArrayList<>();
        if (MyUtils.isStringNotEmpty(department.getName())){
            sql+="and name = ? ";
            objs.add(department.getName());
        }
        return BaseDao.baseQueryCount(sql,objs);
    }

    @Override
    public int addSelect(Department department) {
        List<Object> objs = new ArrayList<>();
        String sql = "insert into department(";
        if (department.getId() !=null){
            sql+="id,";
        }
        if (MyUtils.isStringNotEmpty(department.getName())){
            sql+="name,";
        }
        if (MyUtils.isStringNotEmpty(department.getProfile())){
            sql+="profile,";
        }
        sql= sql.substring(0,sql.lastIndexOf(","));
        sql+=") values(";
        if (department.getId() !=null){
            sql+="?,";
            objs.add(department.getId());
        }
        if (MyUtils.isStringNotEmpty(department.getName())){
            sql+="?,";
            objs.add(department.getName());
        }
        if (MyUtils.isStringNotEmpty(department.getProfile())){
            sql+="?,";
            objs.add(department.getProfile());
        }
        sql= sql.substring(0,sql.lastIndexOf(","));
        sql+=")";
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int updateSelect(Department department) {
        String sql = "update department set ";
        List<Object> objs = new ArrayList<>();
        if (MyUtils.isStringNotEmpty(department.getName())){
            sql+="name = ?, ";
            objs.add(department.getName());
        }
        if (MyUtils.isStringNotEmpty(department.getProfile())){
            sql+="profile = ?, ";
            objs.add(department.getProfile());
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+="where id = ?";
        objs.add(department.getId());
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int batchDelete(int[] ids) {
        List<Object> objs = new ArrayList<>();
        StringBuilder sql = new StringBuilder("delete from department where id in (");
        for (int id : ids){
            sql.append("?,");
            objs.add(id);
        }
        sql=new StringBuilder(sql.substring(0,sql.lastIndexOf(",")));
        sql.append(")");
        return BaseDao.baseBatch(sql.toString(),ids.length,objs);
    }

    @Override
    public Department getById(int id) {
        return BaseDao.baseQueryBean(Department.class,"select * from department where id = ? ",id);
    }
}
